package cn.com.ddapp.ddm.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.base.BaseApplication;


/**
 * Created by caopenghui on 2016-08-05.
 */
public class AddAndSubView extends LinearLayout implements View.OnClickListener {

    private EditText editText;
    private TextView addTextView;
    private TextView subTextView;
    private OnAddAndSubClicker addAndSubClicker;

    public AddAndSubView(Context context) {
        super(context);
    }

    public AddAndSubView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //加载视图的布局
       View view =  LayoutInflater.from(context).inflate(R.layout.add_sub_view,this,true);
        editText = (EditText) view.findViewById(R.id.edittext);
        editText.setText("1");
        editText.setFocusable(false);
        addTextView = (TextView)view.findViewById(R.id.addTextView);
        subTextView = (TextView) view.findViewById(R.id.subTextView);
        if(editText.getText().toString().equalsIgnoreCase("1")){
            subTextView.setTextColor( BaseApplication.getIntstance().getResources().getColor(R.color.action_color));
        }else{
            subTextView.setTextColor( BaseApplication.getIntstance().getResources().getColor(R.color.black));
        }
        subTextView.setOnClickListener(this);
        addTextView.setOnClickListener(this);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if("1".equalsIgnoreCase(editText.getText().toString())){
                    subTextView.setTextColor(BaseApplication.getIntstance().getResources().getColor(R.color.action_color));
                    subTextView.setOnClickListener(null);
                }else{
                    subTextView.setTextColor(BaseApplication.getIntstance().getResources().getColor(R.color.black));
                    subTextView.setOnClickListener(AddAndSubView.this);
                }
            }
        });
    }

    public AddAndSubView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public AddAndSubView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    public void onClick(View view) {
        if(addTextView.getId()==view.getId()){
            editText.setText(Integer.parseInt(editText.getText().toString())+1+"");
            if(addAndSubClicker!=null){
                addAndSubClicker.onAdd();
            }
        } else if(subTextView.getId()==view.getId()){
            editText.setText(Integer.parseInt(editText.getText().toString())-1+"");
            if(addAndSubClicker!=null){
                addAndSubClicker.onSub();
            }
        }
    }

    public interface OnAddAndSubClicker{
        void onAdd();
        void onSub();
    }


    public void setAddAndSubClicker(OnAddAndSubClicker addAndSubClicker) {
        this.addAndSubClicker = addAndSubClicker;
    }

    public Integer getCount(){
        if(editText==null)return 0;
        return Integer.parseInt(editText.getText().toString());
    }

    public void setCount(Integer count){
        if(editText!=null)editText.setText(count+"");
    };
}
